// JavaScript Documentvar logoid="#smallmaplink",ostype="Android";
var page_index=4,maxpage=0;
//非空校验的函数
function checkparam(params){
	var flag=true;
	$.each(params,function(i,ff){
			//alert($(ff)+"----"+$(ff).attr("id"));
			var reg=/[/]/g;
			if(0==$(ff).val().length || $(ff).val()=="" || $(ff).val()==null)
			{
				flag=false;
			}
			if($(ff).val().indexOf("/",0)>=0)
			{
				
				$(ff).val($(ff).val().replace(reg,"!"));
			}
		});
	return flag;
}

function checksize()
{
	if(isNaN($("#packagesize").val()))
	{
		$("#formaterror").css("font-size","13px").text("大小必须是数字!");
		return false;
	}
	return true;
	
}
function inittypes(ostype)
{
	var url1=jsonserver+"appstore/alltypes/ostype/"+ostype;
		$.getJSON(url1+".jsonp?callback=?",{},function(data){
				if(data.appsb.status=="00000")
				{
					$.each(data.tags,function(i,items){
						var option=$("<option>").attr("value",items.typeid).text(items.typename);
						$("#category").append(option);
					});
				}
			});
}
function initlist(ostype,curpage,limit)
{
	$("#showdatas tr:gt(0)").remove();
	//$("tr[id='data']").remove();
	var listurl=jsonserver+"appstore/alldespokeapps/ostype/"+ostype+"/curpage/"+curpage+"/limit/"+limit;
	var datastable=$("#showdatas");
	$.getJSON(listurl+".jsonp?callback=?",{},function(data){
			if(data==null || data.length==0)
			{
				var datastr=$("<tr>");
				$("<td>").attr("colspan","12").attr("align","center").text("暂无数据").appendTo(datastr);
				datastr.appendTo(datastable);
			}
			else
			{
				//alert(data.length+'-------');
				
				$.each(data,function(i,items){
					var datastr=$("<tr>")
						.attr("id","data")
						.css("font-size","13px")
						.mouseover(function(){
							$(this).css("background-color","#9CF").css("cursor","pointer");
						})
						.mouseout(function(){
							$(this).css("background-color","");
						})
						.dblclick(function(){
							setcurapp(items.appid);
						});
					$("<td>").text(items.appid).appendTo(datastr);
					$("<td>").text(items.title).appendTo(datastr);
					$("<td>").text(items.platform).appendTo(datastr);
					$("<td>").text(items.categoryname).appendTo(datastr);
					$("<td>").text(items.version).appendTo(datastr);
					$("<td>").text(items.releasedate).appendTo(datastr);
					var desc=items.description;				
					$("<td>").text(desc.substr(0,15)+"...").appendTo(datastr);
					$("<td>").text(items.fee).appendTo(datastr);
					$("<td>").text(items.packagesize).appendTo(datastr);
					$("<td>").text(items.sign).appendTo(datastr);
					$("<td>").text(items.downloadnumber).appendTo(datastr);
					$("<td>").text(items.packagelink).appendTo(datastr);
					$("<td>").text(items.smallmaplink).appendTo(datastr);
					$("<td style='background-color:#fff;'>").html("<a  id='"+items.appid+"' onClick='del(this)' style='font-size:13px;' >删除</al>").css("cursor","pointer").appendTo(datastr);
					datastr.appendTo(datastable);
					
				});
			}
			
		});
		
}
function setcurapp(appid)
{
	$("#addbtn").hide();
	$("#updatebtn").show();
	var url=jsonserver+"appstore/getdespokeapp/appid/"+appid;
	$.getJSON(url+".jsonp?callback=?",{},function(data){
		if(data==null || data.length==0)
		{
			alert("服务器连繁忙,请稍后再试.");
		}
		else
		{
			$("#appid").val(data.appid).attr("readonly","readonly").css("background-color","#CCC");
			$("#title").val(data.title);
			var radios=$("input[type='radio']");
			//alert(radios.length);
			$.each(radios,function(i,items){
				//alert($(items).val());
				if($(items).val()==data.platform)
				{
					$(items).attr("checked","checked");
				}
			});
			var selects=$("#category").children("option");
			$.each(selects,function(i,items){
				if($(items).text()==data.categoryname)
				{
					$(items).attr("selected","selected");
				}
			});
			$("#downloadnumber").val(data.downloadnumber);
			$("#version").val(data.version);
			$("#releasedate").val(data.releasedate);
			$("#description").val(data.description);
			var ps=data.packagesize;
			$("#packagesize").val(ps.substr(0,ps.length-2));
			var selects=$("#fee").children("option");
			$.each(selects,function(i,items){
				if($(items).val()==data.fee)
				{
					$(items).attr("selected","selected");
				}
			});
			var signselect=$("#sign").children("option");
			$.each(signselect,function(i,items){
			
				if($(items).val()==data.sign)
				{
					$(items).attr("selected","selected");
				}
			});
			$("#packagelink").val(data.packagelink);
			$("#smallmaplink").val(data.smallmaplink);
		}
	});
	
}

function getParamsArray()
{
	var params=new Array();
	params.push($("#appid"));
	params.push($("#title"));
	$("input[type='radio']").attr("id","platform")
	params.push($(":radio[checked=checked]"));
	params.push($("#category"));
	params.push($("#releasedate"));
	params.push($("#fee"));
	params.push($("#version"));
	params.push($("#packagesize"));
	params.push($("#packagelink"));
	params.push($("#smallmaplink"));
	return params;
}
//普通字符串转json字符串
function strToJsonStr(params)
{
	var jsonstr="{";
			$.each(params,function(i,item1){
				var thisvalue=($(item1).val()==null || $(item1).val()=="")?" ":$(item1).val();
				if(i<params.length-1)
				{			
					//alert(item1.attr("id"));
					jsonstr+='\"'+$(item1).attr("id")+'\":\"'+thisvalue+'\",';
				}
				else
				{
					jsonstr+='\"'+$(item1).attr('id')+'\":\"'+thisvalue+'\"';
				}
			});
			jsonstr+="}";
		//alert(jsonstr);
		return jsonstr;
}

function del(obj)
{
	
	if(confirm("确定删除?"))
	{
		setTimeout(function(){
			var deleteurl=jsonserver+"appstore/delete/appid/"+$(obj).attr("id");
				$.getJSON(deleteurl+".jsonp?callback=?",{},function(data){

					var colorstr="red";
					if(data.status=="00000")
					{
						colorstr="#096";
						
					}
					$("#showmsg").css("color",colorstr).text(data.msg).fadeIn(700);
					initlist("all",1,2);
				});
			},1500);
			
		setTimeout(function(){
			$("#showmsg").fadeOut();
		},3000);
		
	}
}
function update(str)
{
	var updateurl=jsonserver+"appstore/updatedespokeapp/newstr/"+str;
	setTimeout(function(){
			$.getJSON(updateurl+".jsonp?callback=?",{},function(data){
				var colorstr="red";
				if(data.status=="00000")
				{
					colorstr="#096";
					$("input[type=text]").val("");
					$("textarea").val("");
					location.reload();
				}
				$("#showmsg").css("color",colorstr).text(data.msg).fadeIn(700);
				
				$("#updatebtn").html("<button>确认修改</button>");
				initlist("all",1,2);
			});
	},1500);
}